package com.yanqu.road.logic.activity.channelmusicroomrank;

import com.yanqu.road.dao.impl.channelcenter.channelmusicroom.ChannelMusicRoomRankActivityRankConfigDaoImpl;
import com.yanqu.road.entity.activity.ActivityConfig;
import com.yanqu.road.entity.activity.ActivityInfo;
import com.yanqu.road.entity.activity.channelmusicroomrank.ChannelMusicRoomRankDetailConfig;
import com.yanqu.road.entity.activity.channelmusicroomrank.CrossChannelMusicRoomRankShowData;
import com.yanqu.road.logic.config.ChannelConfig;
import com.yanqu.road.utils.date.DateHelper;
import com.yanqu.road.utils.property.Property;
import com.yanqu.road.utils.property.PropertyHelper;
import com.yanqu.road.utils.serialize.Item;
import com.yanqu.road.utils.string.StringUtils;

import java.util.*;

public class ChannelMusicRoomRankConfig {

    private ActivityInfo activityInfo;
    /**
     * 每日开放时间
     */
    private Map<Integer, List<Long>> openTimeListMap = new HashMap<>();
    /**
     * 最大演出美女数
     */
    private int maxShowBeautyNum = 100;
    /**
     * 免费演出次数限制
     */
    private int freeShowTimes = 1;
    /**
     * 道具演出次数限制
     */
    private int itemShowTimes = 1;
    /**
     * 道具演出消耗
     */
    private Property itemShowCost = new Property();
    /**
     * 主题加成千分比
     */
    private int themeBuffPercent = 200;
    /**
     * 时辰卡BUFF
     */
    private List<Item> timeCardBuffList = new ArrayList<>();
    /**
     * 演出总分分段配置
     */
    private List<Integer> scoreRankSectionList = new ArrayList<>();

    /**
     * 最大历史演出数
     */
    private int maxHistoryShowNum = 50;

    /**
     * 玩家每场最多参赛数
     */
    private int maxUserBeautyNum = 5;

    private List<ChannelMusicRoomRankDetailConfig> rankConfigList = new ArrayList<>();

    public ChannelMusicRoomRankConfig(ActivityInfo activityInfo, Map<String, ActivityConfig> activityConfigMap) {
        this.activityInfo = activityInfo;
        List<ChannelMusicRoomRankDetailConfig> tempConfigList = new ChannelMusicRoomRankActivityRankConfigDaoImpl(ChannelConfig.CHANNEL_CENTER_DB_PARAM).getRankConfigList(activityInfo.getActivityId());
        this.rankConfigList = tempConfigList;

        this.maxShowBeautyNum = activityConfigMap.get("MUSICROOM_DRAFT_COND_PEOPLE_NUM").getIntValue();
        this.themeBuffPercent = activityConfigMap.get("MUSICROOM_DRAFT_THEME_ADD").getIntValue();
        this.maxUserBeautyNum = activityConfigMap.get("MUSICROOM_DRAFT_BEAUTY_NUM_MAX").getIntValue();
        this.freeShowTimes = activityConfigMap.get("MUSICROOM_DRAFT_JOIN_NUM_MAX_FREE").getIntValue();
        this.itemShowTimes = activityConfigMap.get("MUSICROOM_DRAFT_JOIN_NUM_MAX_ITEM").getIntValue();
        this.itemShowCost = PropertyHelper.parseStringToProperty(activityConfigMap.get("MUSICROOM_DRAFT_JOIN_ITEM_COST").getValue());
        for (String s : activityConfigMap.get("MUSICROOM_DRAFT_TIME_CARD_ADD").getValue().split("\\|")) {
            Item item = Item.newInstance(s, ";");
            timeCardBuffList.add(item);
        }
        for (String s : activityConfigMap.get("MUSICROOM_DRAFT_RANK_POND").getValue().split("\\|")) {
            String[] split = s.split(";");
            int startRank = Integer.valueOf(split[0]);
            int endRank = Integer.valueOf(split[1]);
            this.scoreRankSectionList.add(endRank);
        }

        List<String> tempStrList = activityConfigMap.get("MUSICROOM_DRAFT_ACTIVITY_TIME").getStringListValue("\\|");
        for(String strInfo : tempStrList){
            List<Integer> tempIntList = StringUtils.stringToIntegerList(strInfo,";");
            List<Long> timeList = new ArrayList<>();
            //开始时间
            timeList.add(activityInfo.getBeginTime() + (tempIntList.get(0) - 1) * DateHelper.DAY_SECONDS + tempIntList.get(1) * DateHelper.HOUR_SECONDS);
            //结束时间
            timeList.add(activityInfo.getBeginTime() + (tempIntList.get(0) - 1) * DateHelper.DAY_SECONDS + tempIntList.get(2) * DateHelper.HOUR_SECONDS);

            openTimeListMap.put(tempIntList.get(0), timeList);
        }
    }

    public ActivityInfo getActivityInfo() {
        return activityInfo;
    }

    public int getMaxShowBeautyNum() {
        return maxShowBeautyNum;
    }


    /**
     * 获得时辰卡累计使用次数对应的加成千分比
     */
    public int getCardUseAmountBuffValue(int amount) {
        Item takeItem = null;
        for (Item item : timeCardBuffList) {
            if (amount >= item.getId()) {
                takeItem = item;
            } else {
                break;
            }
        }
        if (takeItem == null) {
            return 0;
        }
        return takeItem.getNum();
    }

    public int getFreeShowTimes() {
        return freeShowTimes;
    }

    public int getItemShowTimes() {
        return itemShowTimes;
    }

    public Property getItemShowCost() {
        return itemShowCost;
    }

    /**
     * 随机一个主题
     *
     * @return
     */
    public int randomTheme() {
        return new Random().nextInt(5) + 1;
    }

    public int getMaxHistoryShowNum() {
//        return maxHistoryShowNum;
        return 9999;
    }

    /**
     * @param inStartTime 秒
     * @return
     */
    public long getRoundEndTime(long inStartTime) {
//        int whichDay = ActivityHelper.getWhichDayAfterBeginTime(activityInfo);
//        List<Long> times = openTimeListMap.get(whichDay);
//        if (times == null) {
//            return activityInfo.getEndTime();
//        }
//        long dayStartTime = times.get(0);
//        if (inStartTime < dayStartTime) {
//            return dayStartTime;
//        }
//        long endTime = times.get(1);
//        if (endTime >= activityInfo.getEndTime()) {
//            endTime = activityInfo.getEndTime();
//        }
        for (List<Long> times : openTimeListMap.values()) {
            long dayStartTime = times.get(0);
            long dayEndTime = times.get(1);
            if (inStartTime >= dayStartTime && inStartTime <= dayEndTime) {
                return dayEndTime;
            }
        }
        return activityInfo.getEndTime();
    }

    /**
     * 计算场景里的评分
     *
     * @param showData
     * @param theme
     * @return
     */
    public long calcThemeScore(CrossChannelMusicRoomRankShowData showData, int theme) {
        long score = 0;
        for (int i = 0; i < showData.getAttrList().size(); i++) {
            long attrValue = showData.getAttrList().get(i);
            if (i == theme - 1) {
                //主题加成
                score += attrValue + attrValue * themeBuffPercent / 1000;
            } else {
                score += attrValue;
            }
        }
        return score;
    }

    /**
     * 计算选拔时的评分,并设置
     *
     * @param showData
     * @param theme
     * @return
     */
    public long calcAndSetShowScore(CrossChannelMusicRoomRankShowData showData, int theme) {
        long showScore = calcThemeScore(showData, theme);
        showData.setShowScore(showScore);
        return showScore;
    }

    public int getMaxUserBeautyNum() {
        return maxUserBeautyNum;
    }

    public List<ChannelMusicRoomRankDetailConfig> getRankConfigList() {
        return rankConfigList;
    }

    public ChannelMusicRoomRankDetailConfig getRankConfigByRank(int rank) {
        for (ChannelMusicRoomRankDetailConfig rankConfig : rankConfigList) {
            if (rank <= rankConfig.getRank()) {
                return rankConfig;
            }
        }
        return null;
    }

    public int getScoreRankListIndex(int scoreRank) {
        for (int i = 0; i < scoreRankSectionList.size(); i++) {
            int compareRank = scoreRankSectionList.get(i);
            if (scoreRank <= compareRank) {
                return i;
            }
        }
        return scoreRankSectionList.size();
    }

    public boolean isInOpenTime() {
        int currentSecond = DateHelper.getCurrentSecond();
        for (List<Long> times : openTimeListMap.values()) {
            long dayStartTime = times.get(0);
            long dayEndTime = times.get(1);
            if (currentSecond >= dayStartTime && currentSecond < dayEndTime) {
                return true;
            }
        }
        return false;
    }
    
}
